package com.chenfan.mcn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.mcn.enums.LiveStarSettlementStatusEnum;
import com.chenfan.mcn.enums.OperateLockEnum;
import com.chenfan.mcn.exception.McnErrorCode;
import com.chenfan.mcn.extension.Res;
import com.chenfan.mcn.extension.excel.ExcelUtils;
import com.chenfan.common.extension.validation.groups.Add;
import com.chenfan.common.extension.validation.groups.Modify;
import com.chenfan.common.extension.lock.DistributedLockTemplate;
import com.chenfan.mcn.model.dto.*;
import com.chenfan.mcn.model.vo.*;
import com.chenfan.mcn.service.LiveStarSettlementService;
import com.chenfan.mcn.utils.BusinessAssert;
import com.chenfan.mcn.utils.CommonUtil;
import com.chenfan.privilege.common.config.SearchAuthority;
import com.chenfan.privilege.common.config.UrlAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 直播红人结算单
 * @author wulg
 * @date 2023/3/21
 */
@RestController
@RequestMapping("/liveStarSettlement")
public class LiveStarSettlementController {

    @Autowired
    private LiveStarSettlementService liveStarSettlementService;

    /**
     * 分页查询-直播红人结算单
     *
     * @param searchDTO
     * @return
     */
    @PostMapping("/getPage")
    @UrlAuth({"settlementForm_search"})
    public Res<Page<LiveStarSettlementListVO>> getPage(@SearchAuthority(filterCustomer = false)  LiveStarSettlementSearchDTO searchDTO) {
        return Res.ok(liveStarSettlementService.getPage(searchDTO));
    }

    /**
     * 导出
     * @param searchDTO
     * @param response
     */
    @PostMapping("/export")
    @UrlAuth({"settlementForm_export"})
    public void export(@SearchAuthority(filterCustomer = false)  LiveStarSettlementSearchDTO searchDTO, HttpServletResponse response) {
        List<LiveStarSettlementExportVO> exportList = CommonUtil.defaultList(liveStarSettlementService.exportExcel(searchDTO));
        ExcelUtils.exportExcel(exportList, "直播红人结算单", LiveStarSettlementExportVO.class, "直播红人结算单.xlsx", response);
    }

    /**
     * 导出明细
     * @param queryDTO
     * @param response
     */
    @PostMapping("/exportDetail")
    public void exportDetail(@Validated @RequestBody LiveStarSettlementDetailQueryDTO queryDTO, HttpServletResponse response) {
        List<LiveStarSettlementCommodityVO> exportList = CommonUtil.defaultList(liveStarSettlementService.exportDetail(queryDTO));
        ExcelUtils.exportExcel(exportList, "直播红人结算明细", LiveStarSettlementCommodityVO.class, "直播红人结算明细.xlsx", response);
    }


    /**
     * 新建-直播红人结算单
     *
     * @param saveDTO
     * @return
     */
    @PostMapping("/add")
    @UrlAuth({"settlementForm_add"})
    private Res<Long> addSettlement(@Validated({Add.class}) @RequestBody LiveStarSettlementSaveDTO saveDTO) {
        if (CommonUtil.isEmptyArray(saveDTO.getAttachmentFile())) {
            saveDTO.setAttachmentFile(null);
        }
        saveDTO.setId(null);
        return Res.ok(liveStarSettlementService.save(saveDTO));
    }

    /**
     * 修改-直播红人结算单
     *
     * @param saveDTO
     * @return
     */
    @PostMapping("/modify")
    private Res<Long> modifySettlement(@Validated({Modify.class})  @RequestBody LiveStarSettlementSaveDTO saveDTO) {
        if (CommonUtil.isEmptyArray(saveDTO.getAttachmentFile())) {
            saveDTO.setAttachmentFile(null);
        }
        return Res.ok(DistributedLockTemplate.execute(OperateLockEnum.LIVE_STAR_SETTLEMENT.generateKey(saveDTO.getId()),
                () -> liveStarSettlementService.save(saveDTO)));
    }

    /**
     * 修改单据状态
     *
     * @param id   结算单ID
     * @param status 单据状态
     * @return {@link Boolean}
     */
    @GetMapping(value = "/changeStatus")
    @UrlAuth({"settlementForm_cancel","settlementForm_submit","settlementForm_withdraw"})
    public Res<Boolean> changeStatus(@RequestParam Long id, @RequestParam Integer status) {
        BusinessAssert.notNull(id, McnErrorCode.PARAM_ERROR, "结算单ID不能为空");
        BusinessAssert.notNull(status, McnErrorCode.PARAM_ERROR, "单据状态不能为空");
        BusinessAssert.notNull(LiveStarSettlementStatusEnum.anyMatch(status,
                LiveStarSettlementStatusEnum.APPROVAL, LiveStarSettlementStatusEnum.CANCEL, LiveStarSettlementStatusEnum.INVALID),
                McnErrorCode.PARAM_ERROR, "非法操作！");
        return Res.ok(DistributedLockTemplate.execute(OperateLockEnum.LIVE_STAR_SETTLEMENT.generateKey(id),
                () -> liveStarSettlementService.changeStatus(id, status, null)));
    }


    /**
     * 详情页
     *
     * @param id 结算单主键ID
     * @param includeDetail 是否包含明细 （用于编辑-取数使用）
     * @return
     */
    @GetMapping("/detail")
    public Res<LiveStarSettlementDetailVO> detail(@RequestParam Long id , @RequestParam(required = false, defaultValue = "false") Boolean includeDetail) {
        BusinessAssert.notNull(id, McnErrorCode.PARAM_ERROR, "结算单ID不能为空");
        return Res.ok(liveStarSettlementService.detail(id, includeDetail));
    }

    /**
     * 分页查询结算单明细
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/getDetailPage")
    public Res<Page<LiveStarSettlementCommodityVO>> getDetailPage(@Validated @RequestBody LiveStarSettlementDetailQueryDTO queryDTO) {
        return Res.ok(liveStarSettlementService.getDetailPage(queryDTO));
    }
}
